package cn.com.bluemoon.daps.notice.api;

import cn.com.bluemoon.daps.api.notice.RemoteNoticeHandlerService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiNotice;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.notice.core.NoticeQueryVo;
import cn.com.bluemoon.daps.notice.core.NoticeType;
import cn.com.bluemoon.daps.notice.core.PreSendMsgInfo;
import cn.com.bluemoon.daps.notice.infrastructure.service.DapSendMailRecordService;
import cn.com.bluemoon.daps.notice.template.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 同步结构中常用邮件接口
 *
 * @author Jarod.Kong
 * @date 2021/6/9 21:26
 */
@RestController
@Slf4j
@RequestMapping(DapsApiNotice.API_NOTICE)
@IgnorePermission(name = "消息接收")
@Api("消息通知接口")
public class NoticeHandlerApi implements RemoteNoticeHandlerService {

    @Autowired
    private DapSendMailRecordService sendMailRecordService;

    @Override
    public ResultBean<NoticeQueryVo> queryNoticeContentById(String msgRecordId) {
        return ResultBean.ok(sendMailRecordService.getById(msgRecordId))
                .convert(r -> {
                    if (r == null) {
                        return null;
                    }
                    NoticeQueryVo res = new NoticeQueryVo();
                    res.setContent(r.getSendContent());
                    res.setSendStatus(r.getSendStatus());
                    res.setSendTime(r.getCreateTime());
                    return res;
                });
    }

    @PostMapping("/presend")
    @Override
    public ResultBean<String> preSendInfo(@RequestParam(value = "type", required = false) NoticeType type,
                                                  @RequestBody PreMetadataImpactTxtMessage baseMsg) {
        String res = sendMailRecordService.preSend(baseMsg).getContent();
        return ResultBean.ok(res);
    }

    @PostMapping("sendMim")
    @Override
    public ResultBean<Boolean> sendMim(@RequestParam(value = "type", required = false) NoticeType type,
                                       @RequestBody MetadataImpactMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

    @PostMapping("sendDs")
    @Override
    public ResultBean<Boolean> sendDevMsg(@RequestParam(value = "type", required = false) NoticeType type,
                                          @RequestBody DevSyncMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

    @PostMapping("sendPs")
    @Override
    public ResultBean<Boolean> sendProMsg(@RequestParam(value = "type", required = false) NoticeType type,
                                          @RequestBody ProSyncMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

    @PostMapping("sendAp")
    @Override
    public ResultBean<Boolean> sendApplyMsg(@RequestParam(value = "type", required = false) NoticeType type,
                                            @RequestBody ApplyPublishMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

    @Override
    public ResultBean<Boolean> sendSecurityApplyAuth(NoticeType type, SecurityTableApplyMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

    @Override
    public ResultBean<Boolean> sendSecurityApplyExport(NoticeType type, SecurityExportApplyMessage baseMsg) {
        return ResultBean.ok(sendMailRecordService.send(baseMsg));
    }

}
